package com.mk.aline.aspectjdemo;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.mk.aline.aspectjdemo.Beans.People;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "test_MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e(TAG, "onCreate: ");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "onResume: ");
        testAOP();
        testAround();
    }

    private void testAround() {
        Log.e("testAround", "testAround: " + "around");
    }

    private void testAOP() {
        Log.e("testAop", "testAOP");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e(TAG, "onStart: ");
    }

    public void click(View view) {
        checkPhoneState();
    }

    @SecurityCheckAnnotation(declaredPermission = "android.permission.READ_PHONE_STATE")
    public void checkPhoneState() {
        //如果不使用AOP，就得自己来检查权限
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED) {
            Log.e(TAG, "have no permission to read phone state");
            return;
        }
        Log.e(TAG, "Read Phone State succeed");
        return;
    }


    public void getPeople(View view) {
        People mPeople = new People("liming", 23);
        Log.e(TAG, "getPeopleStr: " + mPeople.getName());
        Log.e(TAG, "getPeopleInt: " + mPeople.getAge());

        mPeople.setName("llll");
        Log.e(TAG, "getPeople: set" + mPeople.getName());
    }

    public void handleEx(View view) {
        catchMethod();
    }

    private void catchMethod() {
        try {
            int sum = 100 / 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
